var modal_selector = "#modal-table";
var modal_pager = "#modal-pager";
var myEvent;
function showDetail(url, type) {
    var arr = [1, 2, 3, 5, 7];
    var arr2 = [6, 7];
    var colModel = jQuery(modal_selector).jqGrid('getGridParam', 'colModel');
    var colName = colModel[4]['name'];
    var colName2 = colModel[3]['name'];
    if (jQuery.inArray(type, arr2) != -1) {
        jQuery(modal_selector).jqGrid('setLabel', colName2, "设备");
    } else {
        jQuery(modal_selector).jqGrid('setLabel', colName2, "网点");
    }
    if (jQuery.inArray(type, arr) != -1) {
        jQuery(modal_selector).jqGrid('setLabel', colName, "有无计划");
        jQuery(modal_selector).jqGrid('setColProp', 'amount', {
            edittype: "select",
            formatter: "select",
            editoptions: {
                value: '1:有;0:无'
            }
        });
    } else {
        jQuery(modal_selector).jqGrid('setLabel', colName, "金额（元）");
        jQuery(modal_selector).jqGrid('setColProp', 'amount', {
            edittype: "text",
            formatter: "integer",
            editrules: {number: true, minValue: 0},
            editable: true
        });
    }
    jQuery("#modal").modal({backdrop: 'static', keyboard: false});
    jQuery(modal_selector).jqGrid('setGridParam', {
        url: url
    }).trigger('reloadGrid');//重新载入
}

function changeStatus(projectId, id, type, status) {
    jQuery.ajax({
        'url': root + "/plan/changeStatus.do",
        'traditional': true,
        'data': {
            projectId: projectId,
            businessType: type,
            status: status,
            id: id
        },
        'dataType': 'text',
        'type': 'post',
        'success': function (rs) {
            jQuery(modal_selector).trigger("reloadGrid");
        },
        'error': function () {
        },
        'async': true
    });
}

function myButtons(cellvalue, options, rowObject) {
    var edit = "<div onmouseout=\"jQuery(this).removeClass('ui-state-hover')\" onmouseover=\"jQuery(this).addClass('ui-state-hover');\" onclick=\"jQuery.fn.fmatter.rowactions.call(this,'edit');\" class=\"ui-pg-div ui-inline-edit\" style=\"float:left;cursor:pointer;\" title=\"编辑所选记录\"><span class=\"ui-icon ui-icon-pencil\"></span></div>";
    var save = "<div onmouseout=\"jQuery(this).removeClass('ui-state-hover');\" onmouseover=\"jQuery(this).addClass('ui-state-hover');\" onclick=\"jQuery.fn.fmatter.rowactions.call(this,'save');\" class=\"ui-pg-div ui-inline-save\" style=\"float: left;display: none;\" title=\"提交\"><span class=\"ui-icon ui-icon-disk\"></span></div>";
    var cancel = "<div onmouseout=\"jQuery(this).removeClass('ui-state-hover');\" onmouseover=\"jQuery(this).addClass('ui-state-hover');\" onclick=\"jQuery.fn.fmatter.rowactions.call(this,'cancel');\" class=\"ui-pg-div ui-inline-cancel\" style=\"float: left; margin-left: 5px;display: none;\" title=\"取消\"><span class=\"ui-icon ui-icon-cancel\"></span></div>";
    var active = "<div onmouseout=\"jQuery(this).removeClass('ui-state-hover');\" onmouseover=\"jQuery(this).addClass('ui-state-hover');\" onclick=\"changeStatus(" + rowObject.projectId + "," + rowObject.id + "," + rowObject.businessType + "," + 1 + ");\" class=\"ui-pg-div ui-inline-del\" style=\"float:left;margin-left:5px;\" title=\"激活\"><span class=\"ui-icon fa-unlock\"></span></div>";
    var disabled = "<div onmouseout=\"jQuery(this).removeClass('ui-state-hover');\" onmouseover=\"jQuery(this).addClass('ui-state-hover');\" onclick=\"changeStatus(" + rowObject.projectId + "," + rowObject.id + "," + rowObject.businessType + "," + 0 + ");\" class=\"ui-pg-div ui-inline-del\" style=\"float:left;margin-left:5px;\" title=\"禁用\"><span class=\"ui-icon fa-lock\"></span></div>";
    var result = "";
    if (rowObject.status == 0) {
        result = active;
    } else if (rowObject.status == 1) {
        result = edit + save + cancel + disabled;
    } else {
        result = edit + save + cancel;
    }
    return result;
}

function addEvent(projectId, event, date, url) {
    jQuery.ajax({
        url: url,
        cache: false,
        type: 'post',
        data: {
            projectId: projectId,
            startDate: date,
            endDate: date,
            title: event.title,
            className: event.className[0],
            businessType: event.type
        },
        success: function (rs) {
            var aj = formatJsonResponse(rs);
            if (aj.code == 500) {
                pushMessage(aj.message, "error", 3);
            }
            $("#calendar").fullCalendar('removeEvents');
            $("#calendar").fullCalendar('refetchEvents');
            $("#calendar").fullCalendar('unselect');
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
        }
    })
}

function updateEvent(projectId, event, url) {
    jQuery.ajax({
        url: url,
        cache: false,
        type: 'post',
        data: {
            id: event.id,
            startDate: event.start,
            projectId: projectId,
            endDate: event.start,
            businessType: event.type
        },
        success: function (rs) {
            var aj = formatJsonResponse(rs);
            if (aj.code == 500) {
                pushMessage(aj.message, "error", 2);
            }
            $("#calendar").fullCalendar('removeEvents');
            $("#calendar").fullCalendar('refetchEvents');
            $("#calendar").fullCalendar('unselect');
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
        }
    })
}

function deleteEvent(event, url) {
    jQuery.ajax({
        url: url,
        cache: false,
        type: 'post',
        data: {
            id: event.id
        },
        success: function (rs) {
            var aj = formatJsonResponse(rs);
            if (aj.code == 500) {
                pushMessage(aj.message, "error", 2);
            } else {
                $("#calendar").fullCalendar('removeEvents');
                $("#calendar").fullCalendar('refetchEvents');
                $("#calendar").fullCalendar('unselect');
            }
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
        }
    })
}

function savePlanDetail(event, url) {
    var _list = {};
    var details = jQuery(modal_selector).jqGrid('getRowData');
    for (var i = 0; i < details.length; i++) {
        if (details[i].amount != "") {
            _list["planDetails[" + i + "].id"] = details[i].id;
            _list["planDetails[" + i + "].eventId"] = event.id;
            _list["planDetails[" + i + "].terminalId"] = details[i].terminalId;
            _list["planDetails[" + i + "].amount"] = details[i].amount;
            _list["planDetails[" + i + "].businessType"] = event.type;
        }
    }
    jQuery.ajax({
        url: url,
        cache: false,
        type: 'post',
        dataType: "json",
        data: _list,
        success: function (rs) {
            toastMessage(rs, null);
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
            return;
        }
    })
}

function copyEvent(projectId) {
    var year = (new Date()).getFullYear();
    var copyWeek = $("#spinner_start").spinner("value") + 1;
    var pastWeek = $("#spinner_end").spinner("value") + 1;
    jQuery.ajax({
        url: root + "/plan/copyEvents",
        cache: false,
        type: 'post',
        dataType: "json",
        data: {
            copyWeekRange: getXDate(year, copyWeek),
            pastWeekRange: getXDate(year, pastWeek),
            projectId: projectId
        },
        success: function (rs) {
            toastMessage(rs, root + "/plan/detail.do?id=" + projectId);
        },
        error: function () {
            pushMessage("系统繁忙，请稍后再试。", "error", 2);
            return;
        }
    })
}